package com.runfast.waimai.web.controller.Driver_controller;

		import com.runfast.paotui.dao.model.Order;
		import com.runfast.paotui.dao.model.RunfastCuseraddress;
		import com.runfast.paotui.dao.model.RunfastShopper;
		import com.runfast.paotui.service.OrderService;
		import com.runfast.paotui.service.RunfastCuseraddressService;
        import com.runfast.paotui.service.RunfastShopperService;
		import com.runfast.waimai.dao.model.RunfastBusiness;
		import com.runfast.waimai.dao.model.RunfastGoodsSellChildren;
		import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;

		import com.runfast.waimai.service.RunfastBusinessService;

		import com.runfast.waimai.service.RunfastGoodsSellChildrenService;
		import com.runfast.waimai.service.RunfastGoodsSellRecordService;
		import com.runfast.waimai.util.AESUtil;
		import com.runfast.waimai.util.MyCustomDateEditor;
		import com.runfast.waimai.util.MyUtil;
		import org.springframework.amqp.core.AmqpTemplate;
		import org.springframework.stereotype.Controller;
		import org.springframework.ui.ModelMap;
		import org.springframework.web.bind.WebDataBinder;
		import org.springframework.web.bind.annotation.*;

		import javax.annotation.Resource;
		import java.io.IOException;
		import java.text.DateFormat;
		import java.text.ParseException;
		import java.text.SimpleDateFormat;
		import java.util.ArrayList;
		import java.util.Date;
		import java.util.List;
		import java.util.Map;


@Controller
@RequestMapping("/driver/order/*")
public class DriverOrderController extends DriverBaseController {
	@Resource
	private RunfastGoodsSellRecordService goodsSellRecordService;
	//RunfastGoodsSellRecord外卖订单
	@Resource
	private RunfastGoodsSellChildrenService goodsSellRecordChildrenService;
	//RunfastGoodsSellChildren外卖订单记录
	@Resource
	private RunfastBusinessService businessService;
	//商家信息RunfastBusiness
	@Resource
	private RunfastCuseraddressService cuserAddressService;
	@Resource
    private RunfastShopperService shopperService;
	@Resource
	private OrderService orderService;

	@Resource
	private AmqpTemplate rabbitTemplate; //rabbitmq 发送
	@InitBinder//限定只有传参包含Application属性时才执行添加此操作
	public void initBinder(WebDataBinder binder) {
		//binder.setValidator(validator);
		//注册转换器
		binder.registerCustomEditor(Date.class, new MyCustomDateEditor());
	}

//*
//* 订单详细      +商品规格返回
//* @throws IOException

	@RequestMapping(value="detail",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap detail(@RequestParam(value="id",required=false) Integer id,String token) throws IOException{
	    //Integer id 定单id

		ModelMap model = new ModelMap();
		RunfastShopper sp=super.getLoginShopper(token);

		if(sp!=null&&id!=null){
			//将getinto 改成了findByid，应该是orderid查询外卖订单规格信息
			RunfastGoodsSellRecord gr = goodsSellRecordService.get(id);


			if(gr!=null){
				List<RunfastGoodsSellChildren> grclist= goodsSellRecordChildrenService.findByOrderCode(gr.getOrdercode());///查询订单子选项
				//OrderCode定单号
				if(grclist!=null&&grclist.size()>0){

						model.put("GoodsSellRecordChildren", grclist);


				}
				gr=MyUtil.replaceTag(gr); //骑手的坐标定点
				//GoodsSellRecord gr =goodsSellRecordService.findByOrderCode(orderCode);  //订单信息
				Map<String,Object> loc = MyUtil.getGDlocFromBD( gr.getBusinessaddresslng(), gr.getBusinessaddresslat()) ;//设置商家的经度，纬度
				Map<String,Object> userLoc = MyUtil.getGDlocFromBD(gr.getUseraddresslng() , gr.getUseraddresslat());//用户的经度，纬度

//				System.out.println(gr.getUserAddressLng() + gr.getUserAddressLat());
//				System.out.println(userLoc);
				//System.out.println("addr = "+ gr.getUserAddress() + gr.getAddress() );
				if(userLoc != null&&userLoc.size()>0){

					gr.setUseraddresslat(userLoc.get("lat").toString());
					gr.setUseraddresslng(userLoc.get("lng").toString());
//					System.out.println("www设置好坐标lat" + gr.getUserAddressLat() +"lng = " + gr.getUserAddressLng());

				}else{
					System.out.print("转换坐标失败");
				}

				if(loc != null&&loc.size()>0){

					gr.setBusinessaddresslat(loc.get("lat").toString());
					gr.setBusinessaddresslng(loc.get("lng").toString());
					System.out.println("设置好坐标lat" + gr.getBusinessaddresslat() +"lng = " + gr.getBusinessaddresslng());

				}

				else{
					//LOG.info("百度坐标转高德坐标失败");
				}//把商家的坐标转换成高德的坐标
				System.out.println("RunfastShopper的long"+sp.getLongitude());
				System.out.println("RunfastShopper的lat"+sp.getLatitude());
				System.out.println("商家的long"+gr.getBusinessaddresslng());
				System.out.println("商家的lat"+gr.getBusinessaddresslat());
				if(sp.getLongitude()!=null&&sp.getLatitude()!=null&&gr.getBusinessaddresslng()!=null&&gr.getBusinessaddresslat()!=null){
					Double shopdis=MyUtil.getDistance(Double.parseDouble(sp.getLongitude()), Double.parseDouble(sp.getLatitude()), Double.parseDouble(gr.getBusinessaddresslng()), Double.parseDouble(gr.getBusinessaddresslat()));
					System.out.println("RunfastShopper的long"+sp.getLongitude());
					System.out.println("RunfastShopper的lat"+sp.getLatitude());
					System.out.println("商家的long"+gr.getBusinessaddresslng());
					System.out.println("商家的lat"+gr.getBusinessaddresslat());
					System.out.println(shopdis);
					model.put("shopbdis", Math.round(shopdis));//骑手到商家距离
				}
				//把商家的坐标转换成高德的坐标

//				if(gr.getBusinessId()!=null&&ComUtil.isEmpty(gr.getBusinessAddressLat())){
//					Business bus = businessService.get(gr.getBusinessId());
//					if(bus!=null){
//						gr.setBusinessAddressLng(bus.getLongitude());
//						gr.setBusinessAddressLat(bus.getLatitude());
//					}
//				}
//				if(gr.getUserAddressId()!=null&&ComUtil.isEmpty(gr.getUserAddressLat())){
//					CuserAddress addr = cuserAddressService.get(gr.getUserAddressId());
//					if(addr!=null){
//						gr.setUserAddressLat(addr.getLatitude());
//						gr.setUserAddressLng(addr.getLongitude());
//					}
//				}
//				//����---------��ɾ��-----------
//				if(ComUtil.isEmpty(gr.getBusinessAddressLat())){
//					gr.setBusinessAddressLat(""+22.797809);
//				}
//				if(ComUtil.isEmpty(gr.getBusinessAddressLng())){
//					gr.setBusinessAddressLng(""+108.359302);
//				}
//				if(ComUtil.isEmpty(gr.getUserAddressLat())){
//					gr.setUserAddressLat(""+22.797609);
//				}
//				if(ComUtil.isEmpty(gr.getUserAddressLng())){
//					gr.setUserAddressLng(""+108.359202);
//				}
//				//����---------��ɾ��-----------
			}
			//getDistance()为String类型 ，要用double进行比较，round为long类型，结果要为String类型，再次转换
			gr.setDistance(String.valueOf(Math.round(Double.parseDouble(gr.getDistance()))));

			model.put("detail", gr);
		}

		return model;
	}
	/*
	* 历史定单查询
	* */
	@PostMapping("" +
			"" +
			"" +
			"olddir")
	public @ResponseBody ModelMap olddir(String token,Integer a,
										 @RequestParam(value = "page",required = false,defaultValue = "0")Integer page,
										 @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize) {
		ModelMap model = new ModelMap();
		RunfastShopper shopper = super.getLoginShopper(token);
		Integer page1 = page * pagesize;
		if (shopper != null) {
			if (a == 1) {
				List<RunfastGoodsSellRecord> list = goodsSellRecordService.findByShopperIdAndStruts(shopper.getId(), 8, page1, pagesize);
				Integer count = goodsSellRecordService.findByShopperIdAndStrutscount(shopper.getId(), 8);
				if (count > 0) {
					model.put("waimai", count);
					model.put("list", list);
					model.put("success", true);
				} else {
					model.put("success", false);
					model.put("msg", "没有历史定单");
				}

			} else if (a == 2) {
				List<Order> orders = orderService.findByShopperidAndStatus(shopper.getId(), 4, page1, 1000);
				Integer count1 = orderService.findByShopperidAndStatuscount(shopper.getId(), 4);
				List<Order> list1 = new ArrayList<Order>();
				for (Order s : orders) {
					RunfastBusiness runfastBusiness = businessService.findById(s.getFromId());

					RunfastCuseraddress runfastCuser = cuserAddressService.findById(s.getToId());
//					Order o=new Order();
//					if(Order.FromType.NEARBY==s.getFromType()){
//						//附近购买，
//				//		RunfastCuseraddress runfastCuser= cuserAddressService.findById(s.getToId()) ;
//
//						o.setFromType(s.getFromType());
//						o.setToName(runfastCuser.getName());
//						o.setToMobile(runfastCuser.getPhone());
//						o.setToAddress(runfastCuser.getUserAddress()+runfastCuser.getAddress());
//						list1.add(o);
//
//					}
					//		if(Order.FromType.SPECIFIED==s.getFromType()) {
					//指定购买

					s.setFromAddress(runfastCuser.getUserAddress());
					s.setFromLat(Double.valueOf(runfastCuser.getLatitude()));
					s.setFromLng(Double.valueOf(runfastCuser.getLongitude()));
					s.setToName(runfastCuser.getName());
					s.setToMobile(runfastCuser.getPhone());
					s.setToAddress(runfastCuser.getUserAddress() + runfastCuser.getAddress());
//						s.setFromAddress(s.getFromAddress());
//						s.setFromLat(Double.valueOf(s.getFromLat()));
//						s.setFromLng(Double.valueOf(s.getFromLng()));
					list1.add(s);


				}
				if (count1 > 0) {
					model.put("paotui", count1);
					model.put("order", list1);
					model.put("success", true);

				} else {
					model.put("success", false);
					model.put("msg", "没有历史定单");
				}


			} else {
				model.put("success", false);
				model.put("msg", "登陆帐号超时");
			}

		}else {
			model.put("success", false);
			model.put("msg", "登陆帐号超时");
		}
		return model;
	}
	/*
	* 佣金查询 commisson
	*
	 *  */
	@PostMapping("yongjing")
	public @ResponseBody ModelMap yongjing(String token,Integer year ) throws ParseException {
		RunfastShopper shopper=super.getLoginShopper(token);
		ModelMap model=new ModelMap();
		DateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHssmm");

		String year1=String.valueOf(year)+"0101000000";
		String year2=String.valueOf(year)+"1231235959";

		if(shopper!=null){


			Date date1=dateFormat.parse(year1);
			Date date2=dateFormat.parse(year2);
				List<Map<String ,String>>list=goodsSellRecordService.findcommisson(shopper.getId(),date1,date2);

			if(null!=list&&year!=null){
					model.put("success",true);
					model.put("list",list);

				}else {
					model.put("success",false);
					model.put("msg","查无结果");
				}

		}

		return model;
	}
	@PostMapping("paotuiYongjing")//跑腿佣金查询
	public @ResponseBody ModelMap paotuiYongjing(String token,Integer year ) throws ParseException {
		RunfastShopper shopper=super.getLoginShopper(token);
		ModelMap model=new ModelMap();
		DateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHssmm");

		String year1=String.valueOf(year)+"0101000000";
		String year2=String.valueOf(year)+"1231235959";

		if(shopper!=null){


			Date date1=dateFormat.parse(year1);
			Date date2=dateFormat.parse(year2);
			List<Map<String ,String>>list=orderService.findcommisson(shopper.getId(),date1,date2);

			if(null!=list&&year!=null){
				model.put("success",true);
				model.put("list",list);

			}else {
				model.put("success",false);
				model.put("msg","查无结果");
			}

		}

		return model;
	}
}
